package com.example.filecabinet.service.impl;

import com.example.filecabinet.constant.ErrorConstant;
import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.dao.Lattice_infoMapper;
import com.example.filecabinet.dao.Record_infoMapper;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.entity.Record_info;
import com.example.filecabinet.service.RecordService;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 操作记录实现类
 *
 * @author wangy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RecordServiceImpl implements RecordService {
    private static Logger logger = LoggerFactory.getLogger(RecordServiceImpl.class);
    @Autowired
    Lattice_infoMapper latticeInfoMapper;
    @Autowired
    public Record_infoMapper recordInfoMapper;

    /**
     * 保存操作记录
     *
     * @param record
     * @return
     */
    @Override
    public String insertSelective(Record_info record) {
        String operateMode = record.getRecordAccessMode();
        logger.info("操作模式为--->" + operateMode);
        logger.info("记录的业务号为--->" + record.getBusinessNumber());
        BaseHttpRspn result = new BaseHttpRspn(false);
        // 更新内容
        Lattice_info latticeInfo = new Lattice_info();
        if (record.getRecordRemarks1() != null) {
            latticeInfo.setLatticeSwithStatus(record.getRecordRemarks1());
        }

        if (operateMode.equals(RecordConstant.RECORD_ACCESS_MODE.TAKE)) {
            latticeInfo.setBusinessNumber("");
            latticeInfo.setBusinessType("");
            latticeInfo.setLatticeRemarks1("");
            latticeInfo.setLatticeOperator("");
            latticeInfo.setLatticeLdleStatus(RecordConstant.LATTIC_LDLE_STATUS.EMPTY);
        } else {
            if (operateMode.equals(RecordConstant.RECORD_ACCESS_MODE.STORE)) {
                latticeInfo.setLatticeLdleStatus(RecordConstant.LATTIC_LDLE_STATUS.USING);
            } else if (operateMode.equals(RecordConstant.RECORD_ACCESS_MODE.REPAIR)) {
                latticeInfo.setLatticeSwithStatus(RecordConstant.LATTICE_SWITH_STATUS.ERROR);
            }
            latticeInfo.setBusinessNumber(record.getBusinessNumber());
            latticeInfo.setBusinessType(record.getBusinessType());
            latticeInfo.setLatticeRemarks1(record.getRecordIdNumber());
            latticeInfo.setLatticeOperator(record.getRecordOperationType());
        }
        latticeInfo.setLatticeUpdateTime(record.getRecordOperationTime());

        record.setRecordRemarks1("");
        // 更新条件
        Lattice_infoExample latticeInfoExample = new Lattice_infoExample();
        Lattice_infoExample.Criteria criteria = latticeInfoExample.createCriteria();
        criteria.andLatticeNumberEqualTo(record.getLatticeNumber());
        criteria.andDeviceNumberEqualTo(record.getDeviceNumber());

        System.out.println("修改的格子信息：" + JacksonFactory.writeJson(latticeInfo));
        System.out.println("保存使用记录的参数：" + JacksonFactory.writeJson(record));
        int latticeResult = latticeInfoMapper.updateByExampleSelective(latticeInfo, latticeInfoExample);
        int recordResult = recordInfoMapper.insertSelective(record);

        if (latticeResult != 0 && recordResult != 0) {
            result.setResult(true);
        } else {
            result.setResult(false);
            result.setRespMsg(ErrorConstant.DATABASE.RECORD_ERRPOR);
        }
        return JacksonFactory.writeJson(result);

    }
}
